<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Asin extends AdminController{
	public $_validation = null;
	
	function __construct(){
		parent::__construct();
		$this->load->model('MAsin');
		
		$this->_validation = Array(
			array('field'=>'asin', 'label'=>'ASIN', 'rules'=>'trim|required|max_length[255]')
			,array('field'=>'creator_id', 'label'=>'创建者', 'rules'=>'required|integer|max_length[11]')
			,array('field'=>'country', 'label'=>'国家', 'rules'=>'trim|required|max_length[4]')
			,array('field'=>'use_times', 'label'=>'使用次数', 'rules'=>'max_length[255]')
			,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
			,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
		);
		
		$this->load->model('MAdmin');
		$allReviewAdmins = $this->MAdmin->getAllReviewAdmin();
		$this->assign('allReviewAdmins', $allReviewAdmins);
		$this->assign('currentAdminId',$this->_user->id);
		$this->assign('currentAdminName',$this->_user->account);
	}
	
	function index($game_tag=null){
		//$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('a.asin','where');
		$this->formfilter->addFilter('a.creator_id','where');
		$this->formfilter->addFilter('a.status','where');
		
		
		if($begintime = filterValue('asin_create_time_begin')){
		    $this->formfilter->addFilter('asin_create_time_begin', 'where',array('a.create_time >= ',strtotime($begintime.' 00:00:00')));
		}
		if($endtime = filterValue('asin_create_time_end')){
		    $this->formfilter->addFilter('asin_create_time_end', 'where',array('a.create_time <= ',strtotime($endtime.' 23:59:59')));
		}
		
		$asin_number_perpage = filterValue('asin_number_perpage');
		$asin_order_type = filterValue('asin_order_type');
		
		$filterCount = $this->MAsin->getCount();  //过滤数量
		$limit = $this->pagination($filterCount,'',$asin_number_perpage);
		$list = $this->MAsin->getList($limit,$asin_order_type);
		
		$this->assign('list', $list);
		$this->assign('filterCount', $filterCount);
		$this->assign('totalCount', $this->MAsin->getTotalCount());
		$this->layout();
	}
	
	function add(){
	    $obj = $this->MAsin->createVo();
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
	        if($this->MAsin->add($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }
	    
	    $this->assign('obj', $obj);
	    $this->layout();
	}
	
	function edit($id){
	    if(!is_numeric($id)){
	        show_error(l('id_not_null'));
	    }
	    $obj = $this->MAsin->getOne(Array('id'=>$id));
	    if(!$obj){
	        errorAndRedirect('未找到定义！');
	    }
	    $this->editObj = $obj;
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
	        if($this->MAsin->update($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }
	    $this->assign('obj', $obj);
	    $this->layout();
	}
	function delete($id, $update_time){
	    if(!is_numeric($id) || !is_numeric($update_time)){
	        show_error(l('id_not_null'));
	    }
	    if(!$this->MTask->delete($id, $update_time)){
	        errorAndRedirect(l('data_fail'));
	    }
	    successAndRedirect(l('delete_success'));
	}
	
	/**
	 * 处理excel表导入
	 */
	function excelImportHandler(){
	    $redirectUrl = site_url($this->_thisModule.$this->_thisController);
	    if ($this->_user->role_id != 3 && $this->_user->role_id != 1){  //非测评组人员或超管
	        errorAndRedirect('非测评组人员或超管不能上传asin数据！！！',$redirectUrl);
	    }
	    
	    $config['upload_path'] = getcwd().DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'excelfiles'.DIRECTORY_SEPARATOR;
	    $config['allowed_types'] = 'xls|xlsx';
	    $config['overwrite'] = false;
	    $config['max_size'] = 100000;
	    
	    $this->load->library('upload',$config);
	    if(!$this->upload->do_upload('excel_file')){
	        $error = $this->upload->display_errors();
	        errorAndRedirect($error,$redirectUrl);
	    }
	    $upload_data = $this->upload->data();
	    $file = $upload_data['full_path'];
	    
	    $this->load->library('PHPExcel');
	    $this->load->library('PHPExcel/IOFactory');
	    
	    $objPHPExcel = new IOFactory();    // 创建对象
	    
	    $readerType = $objPHPExcel::identify($file);
	    $objReader = $objPHPExcel::createReader($readerType);
	    $objPHPExcel = $objReader->load($file);    // 读文件
	    $objWorksheet = $objPHPExcel->getActiveSheet(0);
	    $highestRow = $objWorksheet->getHighestRow();  // 总行数
	    $highestColumn = $objWorksheet->getHighestColumn();    // 总列数
	    $highestColumnIndex = range('A', $highestColumn);
	    
	    // 从第二行开始，第一行一般是表头
	    for ($row = 2; $row <= $highestRow; $row++) {
	        $array = array();
	        foreach ($highestColumnIndex as $value) {
	            $address = $value . $row;
	            $array[] = $objWorksheet->getCell($address)->getFormattedValue();
	        }
	        $newItemArray = array(
	            $array[0],
	            $this->_user->id,
	            $array[1],
	            time(),
	            time()
	        );
	        $sql = 'INSERT IGNORE INTO `core_asin` (`asin`,`creator_id`,`country`,`create_time`,`update_time`) VALUES (?,?,?,?,?)';
	        $this->db->query($sql,$newItemArray);
	    }
	    successAndRedirect('导入ASIN数据成功！');
	    
	}
	
	
}